import type { PageParam, PageResult } from '@vben/request';

import { requestClient } from '#/api/request';

export namespace SystemUserApi {
  /** 用户信息 */
  export interface User {
    id?: number;
    username: string;
    nickname: string;
    text?: string;
    deptId: number;
    postIds: string[];
    email: string;
    mobile: string;
    sex: number;
    avatar: string;
    loginIp: string;
    status: number;
    remark: string;
    createTime?: Date;
    cardType?: string;
    fullName?: string;
    idcard?: string;
    unitId?: number;
    areaCode?: string;
    systemPermissions?: string[];
  }

  /** 分配用户组织请求 */
  export interface AssignUserOrgReq {
    userId: number;
    orgIds: number[];
  }
}

/** 查询用户管理列表 */
export function getUserPage(
  params: PageParam,
  desensitize: boolean,
  encrypt: boolean,
) {
  return requestClient.get<PageResult<SystemUserApi.User>>(
    '/system/user/page',
    {
      params,
      desensitize,
      encrypt,
    },
  );
}
/** 查询管理员用户分页列表（简单权限控制） */
export function getAdminUserPageSimplePermission(
  params: PageParam,
  desensitize: boolean,
  encrypt: boolean,
) {
  return requestClient.post<PageResult<SystemUserApi.User>>(
    '/system/user/page-simple-permission',
    params,
    {
      desensitize,
      encrypt: true,
    },
  );
}

/** 根据角色获得用户分页列表 */
export function getUserPageByRole(
  params: PageParam,
  desensitize: boolean,
  encrypt: boolean,
) {
  return requestClient.get<PageResult<SystemUserApi.User>>(
    '/system/user/page-role',
    {
      params,
      desensitize,
      encrypt,
    },
  );
}
/** 获取该角色下未绑定的用户分页列表 */
export function getUserPageNotRole(
  params: PageParam,
  desensitize: boolean,
  encrypt: boolean,
) {
  return requestClient.get<PageResult<SystemUserApi.User>>(
    '/system/user/page-not-role',
    {
      params,
      desensitize,
      encrypt,
    },
  );
}

/** 查询用户详情 */
export function getUser(id: number) {
  return requestClient.get<SystemUserApi.User>(`/system/user/get?id=${id}`, {
    encrypt: true,
  });
}

/** 新增用户 */
export function createUser(data: SystemUserApi.User) {
  return requestClient.post('/system/user/create', data);
}

/** 修改用户 */
export function updateUser(data: SystemUserApi.User) {
  return requestClient.put('/system/user/update', data);
}

/** 删除用户 */
export function deleteUser(id: number) {
  return requestClient.delete(`/system/user/delete?id=${id}`);
}

/** 批量删除用户 */
export function deleteUserList(ids: number[]) {
  return requestClient.delete(`/system/user/delete-list?ids=${ids.join(',')}`);
}

/** 导出用户 */
export function exportUser(params: any) {
  return requestClient.download('/system/user/export-excel', { params });
}

/** 下载用户导入模板 */
export function importUserTemplate() {
  return requestClient.download('/system/user/get-import-template');
}

/** 导入用户 */
export function importUser(file: File, updateSupport: boolean) {
  return requestClient.upload('/system/user/import', {
    file,
    updateSupport,
  });
}

/** 用户密码重置 */
export function resetUserPassword(id: number, password: string) {
  return requestClient.put('/system/user/update-password', { id, password });
}

/** 用户状态修改 */
export function updateUserStatus(id: number, status: number) {
  return requestClient.put('/system/user/update-status', { id, status });
}

/** 获取用户精简信息列表 */
export function getSimpleUserList() {
  return requestClient.get<SystemUserApi.User[]>('/system/user/simple-list');
}

/** 更新用户系统级权限 */
export function updateUserSystemPermissions(
  id: number | undefined,
  systemPermissions: string[],
) {
  return requestClient.post('/system/user/update-permissions', {
    id,
    systemPermissions,
  });
}

/** 查询用户拥有的组织数组 */
export async function getUserOrgList(userId: number) {
  return requestClient.get(`/system/user-org/list-user-org?userId=${userId}`);
}

/** 赋予用户组织 */
export async function assignUserOrg(data: SystemUserApi.AssignUserOrgReq) {
  return requestClient.post('/system/user-org/assign-user-org', data);
}
